﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using AjaxControlToolkit;
using BizElements.Core;
using BizElements.BusinessLayer;

namespace BizElements.Web
{
    /// <summary>Represents a binding between an entity and its parent entities.</summary>
    public interface IParentBinding
    {
        #region Properties.

        /// <summary>Foreign key property name. Only simple single-field FKs are supported.</summary>
        string ForeignKeyPropertyName { get; }

        /// <summary>Bound control ID: lookup or list control.</summary>
        string BoundControlId { get; }

        /// <summary>Parent form URL.</summary>
        string ParentFormUrl { get; }

        /// <summary>Search provider builder control ID if lookup control is bound to FK.</summary>
        string SeachProviderBuilderControlId { get; }

        /// <summary>Optional create new parent control ID.</summary>
        string CreateNewParentControlId { get; }

        /// <summary>Optional create open parent details control ID.</summary>
        string OpenParentDetailsControlId { get; }

        /// <summary>Indicates whether bound control should render UI that allows users to clear selection or select a <b>null</b> value.</summary>
        bool CanClearSelection { get; }

        #endregion

        #region Methods.

        /// <summary>Returns metadata of table that contains parent records.</summary>
        /// <returns>Table metada.</returns>
        IDbTable GetParentTable();

        /// <summary>Return basic entity descriptor.</summary>
        /// <returns>An object that implements IDataObjectDescriptor interface.</returns>
        IDataObjectDescriptor GetDataObjectDescriptor();

        /// <summary>Tries to get descriptor of default business object imlementation for the parent table.</summary>
        /// <returns>Descriptor od default business layer class for parent table, typically a concrete class without "Entity" suffix; <b>null</b> if no default concrete implementation exists.</returns>
        IBusinessObjectDescriptor TryGetBusinessObjectDescriptor();

        /// <summary>If configured, ie. if lookup control is used, then creates a new instance of search engine builder class.</summary>
        /// <returns>An ISearchEngineBuilder class or null if FK is bound to a list control.</returns>
        ISearchEngineBuilder NewSearchEngineBuilder();

        /// <summary>If bound to list item then returns code list.</summary>
        /// <param name="actor">Actor requesting data.</param>
        /// <returns>A code list or null if lookup control is used.</returns>
        ICodeList GetCodeList(IActor actor);

        /// <summary>Gets FK value selected in bound control.</summary>
        /// <param name="parentBizobjControl">A user control that contains bound lookup or list control.</param>
        /// <returns>FK value or null if nothing is selected.</returns>
        string GetSelectedValue(Control parentBizobjControl);

        /// <summary>Gets text selelected in bound control.</summary>
        /// <param name="parentBizobjControl">A user control that contains bound lookup or list control.</param>
        /// <returns>Default parent property value or null if nothing is selected.</returns>
        string GetText(Control parentBizobjControl);

        /// <summary>Gets lookup control to whick FK field is bound.</summary>
        /// <param name="parentBizobjControl">A user control that contains bound lookup or list control.</param>
        /// <returns>Lookup control or null if list control is used.</returns>
        ILookupControl GetLookupControl(Control parentBizobjControl);

        /// <summary>Gets list control to whick FK field is bound.</summary>
        /// <param name="parentBizobjControl">A user control that contains bound lookup or list control.</param>
        /// <returns>List control or null if lookup control is used.</returns>
        IBoundListControl GetListControl(Control parentBizobjControl);

        /// <summary>Gets search provider builder component.</summary>
        /// <param name="parentBizobjControl">A user control that contains bound lookup or list control.</param>
        /// <returns>Provider builder control or null if list control is used.</returns>
        SearchProviderBuilder GetSearchProviderBuilderComponent(Control parentBizobjControl);

        /// <summary>Gets control that creates a new parent entity via another form.</summary>
        /// <param name="parentBizobjControl">Parent user control.</param>
        /// <returns>Control or null if not configured.</returns>
        Control GetCreateNewParentControl(Control parentBizobjControl);

        /// <summary>Gets control that opens parent details in anoother form.</summary>
        /// <param name="parentBizobjControl">Parent user control.</param>
        /// <returns>Control or null if not configured.</returns>
        Control GetOpenParentDetailsControl(Control parentBizobjControl);

        #endregion
    }
}
